<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>手机号码归属地查询案例</title>
    <script src="./code/lib/jquery.js"></script>
    <style>
      body {
        padding: 0;
        margin: 0;
        margin-top: 50px;
        display: flex;
        justify-content: center;
      }
      body input {
        height: 30px;
        font-size: 24px;
      }
      body button {
        vertical-align: top;
        font-size: 24px;
      }
      body table {
        width: 400px;
        margin-top: 20px;
      }
    </style>
  </head>
  <body>
    <div>
      <input type="text" placeholder="请输入手机号码" id="phoneInput" />
      <button id="searchBtn">查询</button>
      <table border="1">
        <thead>
          <tr>
            <th colspan="2">查询结果</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td width="100">手机号码段</td>
            <td id="phone"></td>
          </tr>
          <tr>
            <td>卡号归属地</td>
            <td id="addr"></td>
          </tr>
          <tr>
            <td>运营商</td>
            <td id="company"></td>
          </tr>
          <tr>
            <td>区号</td>
            <td id="areacode"></td>
          </tr>
          <tr>
            <td>邮编</td>
            <td id="zip"></td>
          </tr>
        </tbody>
      </table>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
      $(function () {
        $("#searchBtn").on("click", function () {
          let phone = $("#phoneInput").val();
          if (phone.length <= 0) return alert("请输入手机号码");
          let sublim = /^1[3-9]\d{9}$/;
          if (!sublim.test($("#phoneInput").val()))return alert("格式不正确,请重新输入");
          $.ajax({
              type:'GET',
              url:'https://www.baifubao.com/callback',
              data:{phone,cmd:'1059'},
              dataType: 'jsonp',
              success: res => {
                  console.log(res);
                  $('#phone').html(phone)
                  $('#addr').html(res.data.area)
                  $('#company').html(res.data.operator)
                  $('#areacode').html(res.data.area_operator)
              }
          })
        });
      });
    </script>
  </body>
</html>
